const MAILER = require('nodemailer');
const { email } = require('../../config')

// 邮件发送
async function sendEmail(to, from, authCode, sendCode, host) {
    if (!to || !authCode) return false;

    from = from || email.sender;
    sendCode = sendCode || '';
    host = host || 'smtp.qq.com'; // 暂只支持QQ邮箱

    try {
        let transporter = MAILER.createTransport({
            host,
            secure: true,
            auth: { user: from, pass: authCode },
            tls: {
                rejectUnauthorized: false
            }
        })
        let info = await transporter.sendMail({
            from: `来自 <${from}>`,
            to: to,
            subject: '验证码',
            html: `
            <div id="contentDiv" onmouseover="getTop().stopPropagation(event);" onclick="getTop().preSwapLink(event, 'spam', 'ZC0011_fuDNBdOMcKsuRWQACumkxd6');" style="position:relative;font-size:14px;height:auto;padding:15px 15px 10px 15px;z-index:1;zoom:1;line-height:1.7;" class="body">    <div id="qm_con_body"><div id="mailContentContainer" class="qmbox qm_con_body_content qqmail_webmail_only" style="opacity: 1;">
                <div style="text-align:center;min-width:640px;width:100%;height:100%;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,'Helvetica Neue','PingFang SC','Microsoft Yahei',Arial,'Noto Sans',sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol','Noto Color Emoji';margin:0;padding:0" bgcolor="#fff">
                    <table border="0" cellpadding="0" cellspacing="0" id="m_-7700660819561398294body" style="text-align:center;width:520px;margin:16px auto; padding: 24px 24px 40px; border-style: solid; border-top-color: #9373EE; border-top-width: 4px; border-radius: 0 0 8px 8px; box-shadow: #c1c1c1 0px 0px 1px;" bgcolor="#fff">
                        <tbody>
                            <tr>
                                <td>
                                    <div style="margin: 80px auto 32px ; font-size: 32px; font-weight: 600; line-height: 45px; letter-spacing: 0px">Mood Blog邮箱账号验证码</div>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <div style="margin: 0 auto; font-size: 18px; line-height: 1.4; left: 0px; top: 77px; font-weight: 400;">你正在进行邮箱验证操作，验证码为: </div>
                                    <div style="padding: 16px; text-align: center; background: rgba(147, 115, 238, 0.04); border-radius: 4px; font-weight: 600; font-size: 24px; line-height: 140%; color: #9373EE; margin: 24px 0;">${sendCode}</div>
                                </td>
                            </tr>
                            <tr>
                                <td style="font-size:13px;line-height:1.6;color:#5c5c5c;padding:25px 0;">
                                    <ul style="text-align:center; margin-top: 24px; margin-bottom: 40px;">
                                        <li style="display: inline-block; color: rgba(24, 24, 25, 0.42); font-size: 14px; line-height: 20px; ">此验证码 5 分钟内有效</li> <br>
                                        <li style="display: inline-block; margin-top: 8px; color: rgba(24, 24, 25, 0.42); font-size: 14px; line-height: 20px;">如非本人操作</li> <br>
                                        <li style="display: inline-block; margin-top: 8px; color: rgba(24, 24, 25, 0.42); font-size: 14px; line-height: 20px;">转给他人将导致账号被盗和个人信息泄漏，谨防诈骗</li>
                                    </ul>
                                </td>
                            </tr>
                            <tr>
                                <td style="font-size:13px;line-height:1.6;color:#5c5c5c;padding:25px 0">
                                    <div style="color: rgba(24, 24, 25, 0.42); font-size: 14px; line-height: 20px; margin-top: 32px;"><a href="https://www.hyl.wiki" target="_blank" style="color: #9373EE; text-decoration: none;" rel="noopener">Mood Blog System</a> - 博客系统</div>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            <style type="text/css">.qmbox style, .qmbox script, .qmbox head, .qmbox link, .qmbox meta {display: none !important;}</style></div></div><!-- --><style>#mailContentContainer .txt {height:auto;}</style>  </div>
              `
        })
        return true
    } catch (error) {
        return error
    }
}



module.exports = {
    sendEmail,
}